import axios from 'axios'
const book ={
    state:{
        bookList:[],
        bkPagination:{
            currentPage:1,
            pageSize:3
        },
        totalPage:0
    },
    mutations:{
        getBookData(state,obj){
            state.bookList = obj
        },
        getBkPageNum(state,num){
            state.totalPage = num
        },
        setBkCurrentPage(state,num){
            state.bkPagination.currentPage = num
        }
    },
    actions:{
        getBookList({commit,state}){
            axios.get('userbook',{
                params:{
                    transform:1,
                    order:'id,desc',
                    page:state.bkPagination.currentPage+','+state.bkPagination.pageSize

                }
            })
            .then( res =>{
                let num = Math.ceil(res.data._results/state.bkPagination.pageSize)
                commit('getBookData',res.data.userbook)
                commit('getBkPageNum',num)
            })
            .catch( err =>{
                console.log(err)
            }
            )
        },
        postBookData({commit},d){
            axios.post('userbook',d.obj)
            .then( res =>{
               if(res.data){
                   d.router.push('/home/book')
               }
            })
            .catch( err =>{
                console.log(err)
            })
        }
    }
}
export default book